import {
  getPgoodsTable,
  editPgoodsRecord
} from '../../api/task'
import {
  PGOODS_LIST,
  PGOODS_CURRENT_ROW,
  PGOODS_DATA_COUNT,
  PGOODS_HISTORY_DATA,
  PGOODS_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    pgoodsList: [],
    pgoodsDataCount: null,
    pgoodsPageSize: 20,
    pgoodsHistoryData: [],
    row: {
      host: '',
      name: '',
      subtitle: '',
      unit: '',
      peopnum: '',
      img: '',
      price: '',
      pprice: '',
      content: '',
      num: '',
      state: '',
      btime: '',
      etime: '',
      ctime: '',
      salcnt: '',
      time: [],
      topState: false
    }
  },
  getters: {
    pgoodsList: state => state.pgoodsList
  },
  mutations: {
    [PGOODS_LIST] (state, data) {
      state.pgoodsList = data
    },
    [PGOODS_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [PGOODS_DATA_COUNT] (state, data) {
      state.pgoodsDataCount = data
    },
    [PGOODS_HISTORY_DATA] (state, data) {
      state.pgoodsHistoryData = data
    },
    [PGOODS_PAGE_SIZE] (state, data) {
      state.pgoodsPageSize = data
    }
  },
  actions: {
    async getPgoodsList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getPgoodsTable(data)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          host: host,
          name: item.name,
          subtitle: item.subtitle,
          unit: item.unit,
          peopnum: item.peopnum,
          img: item.img,
          price: item.price,
          pprice: item.pprice,
          content: item.content,
          num: item.num,
          state: item.state,
          btime: item.btime,
          etime: item.etime,
          ctime: item.ctime,
          salcnt: item.salcnt,
          time: [item.btime, item.etime],
          topState: item.top === 1
        }]
      })
      commit(PGOODS_LIST, newData)
      commit(PGOODS_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.pgoods.pgoodsPageSize) {
        commit(PGOODS_HISTORY_DATA, newData)
      } else {
        commit(PGOODS_HISTORY_DATA, newData.slice(0, rootState.pgoods.pgoodsPageSize))
      }
      return resp
    },
    async editPgoodsData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editPgoodsRecord(data)
      return resp
    },
    async getPgoodsCurrentRow ({ commit, rootState }, row) {
      commit(PGOODS_CURRENT_ROW, row)
    },
    async changePgoodsPage ({ commit, rootState }, index) {
      let data = rootState.pgoods.pgoodsList
      let _start = (index - 1) * rootState.pgoods.pgoodsPageSize
      let _end = index * rootState.pgoods.pgoodsPageSize
      commit(PGOODS_HISTORY_DATA, data.slice(_start, _end))
    },
    async changePgoodsPageSize ({ commit, rootState }, value) {
      commit(PGOODS_PAGE_SIZE, value)
    }
  }
}
